package com.myhexin.entity;

import com.myhexin.constant.TableState;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by zjh on 2017/3/23.
 */
@Getter
@Setter
public class Table {
    /**
     * 桌子上的玩家
     */
    List<Player> players;

    /**
     * 桌上剩下的牌
     */
    LinkedList<Byte> cardsInTable;

    /**
     * 桌子状态
     */
    Integer tableState;

    /**
     * 游戏id
     */
    Integer gameId;

    /**
     * 玩法
     */
    Integer rule;

    /**
     * 出牌玩家的序号
     */
    Integer chuPlayerIndex;

    /**
     * 庄家的序号
     */
    Integer zhuangPlayerIndex;


    /**
     * 一共多少圈的桌子
     */
    Integer totalQuanNum;

    /**
     * 第几圈
     */
    Integer quanNum;

    public void Table(LinkedList<Byte> cardsInTable,Integer rule,Integer totalQuanNum) {
        this.players = new ArrayList<>();
        this.cardsInTable = cardsInTable;
        this.rule = rule;
        this.totalQuanNum = totalQuanNum;
    }

    public void sendCards(){
        int size = this.players.size();
        for (int i=0;i<13;i++) {
            for (int j=0;j<size;j++){
                int playerIndex = (zhuangPlayerIndex + j) / size;
                Byte pop = cardsInTable.pop();
                this.players.get(playerIndex).getCardsInHand().add(pop);
            }
        }
        this.chuPlayerIndex = zhuangPlayerIndex;
        this.quanNum = 1;
    }

    public boolean startGame() {
        if (++this.quanNum > this.totalQuanNum) {
            this.tableState = TableState.Invalid;
            return false;
        }
        this.players.get(chuPlayerIndex);
        return true;
    }


    public Byte grabCard(){
        return cardsInTable.pop();
    }
}
